package com.itmk.web.user_clean.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.user_clean.entity.UserClean;
import com.itmk.web.user_clean.entity.UserCleanParm;
import com.itmk.web.user_clean.service.UserCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 清洁管理模块
 */
@RestController
@RequestMapping("/api/userClean")
public class UserCleanController {
    @Autowired
    private UserCleanService userCleanService;

    /**
     * 我的清洁列表
     */
    @GetMapping("/myList")
    public ResultVo getMyList(UserCleanParm userCleanParm){
        //构造查询条件
        QueryWrapper<UserClean> query = new QueryWrapper<>();
        query.lambda().eq(UserClean::getUserId,userCleanParm.getUserId())
                .like(UserClean::getCleanContent,userCleanParm.getCleanContent())
                .like(UserClean::getStatus,userCleanParm.getStatus())
                .orderByDesc(UserClean::getApplyTime);
        //构造分页对象
        IPage<UserClean> page = new Page<>();
        page.setSize(userCleanParm.getPageSize());
        page.setCurrent(userCleanParm.getCurrentPage());
        IPage<UserClean> list = userCleanService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    /**
     * 清洁列表(物业管理部人员)
     */
    @GetMapping("/list")
    public ResultVo getList(UserCleanParm userCleanParm){
        //构造查询条件
        QueryWrapper<UserClean> query = new QueryWrapper<>();
        query.lambda().like(UserClean::getCleanContent,userCleanParm.getCleanContent())
                .like(UserClean::getStatus,userCleanParm.getStatus())
                .orderByDesc(UserClean::getApplyTime);
        //构造分页对象
        IPage<UserClean> page = new Page<>();
        page.setSize(userCleanParm.getPageSize());
        page.setCurrent(userCleanParm.getCurrentPage());
        IPage<UserClean> list = userCleanService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    /**
     * 新增
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:myClean:add')")
    public ResultVo add(@RequestBody UserClean userClean){
        userClean.setApplyTime(new Date());
        userClean.setStatus("0");
        boolean b = userCleanService.save(userClean);
        if(b){
            return ResultUtils.success("清洁申请成功!");
        }
        return ResultUtils.error("清洁申请失败!");
    }

    /**
     * 编辑
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:myClean:edit')"+"|| hasAuthority('sys:cleanList:do')")
    public ResultVo edit(@RequestBody UserClean userClean){
        boolean b = userCleanService.updateById(userClean);
        if(b){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAuthority('sys:myClean:delete')")
    @DeleteMapping("/{cleanId}")
    public ResultVo delete(@PathVariable("cleanId") Long cleanId){
        boolean b = userCleanService.removeById(cleanId);
        if(b){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }
}